package com.briup.jz.web.controller;

import java.util.List;
import javax.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.briup.jz.bean.BaseUser;
import com.briup.jz.bean.extend.BaseUserExtend;
import com.briup.jz.service.IBaseUserRoleService;
import com.briup.jz.service.IBaseUserService;
import com.briup.jz.utils.Message;
import com.briup.jz.utils.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(description="用户相关接口")
@Validated
@RestController
@RequestMapping("/baseuser")
public class BaseUserController {
	  	@Autowired
	    private IBaseUserService iBaseUserService;
	  	@Autowired
	    private IBaseUserRoleService iBaseUserRoleService;
	    @ApiOperation(value = "查询所有用户")
	    @GetMapping("query")
	    public Message query(String username){
	        List<BaseUser> list = iBaseUserService.query(username);
	        return MessageUtil.success(list);
	    }

	    @ApiOperation(value = "通过id删除")
	    @GetMapping("deleteById")
	    @ApiImplicitParams({
	        @ApiImplicitParam(name="id",value = "主键",paramType = "query", required=true),
	    })
	    public Message deleteById(@NotNull Long id){
	    	iBaseUserService.deleteById(id);
	        return MessageUtil.success("删除成功");
	    }

	    @ApiOperation(value = "保存或更新用户信息")
	    @PostMapping("saveOrUpdate")
	    public Message saveOrUpdate(BaseUser baseUser){
	    	iBaseUserService.saveOrUpdate(baseUser);
	        return MessageUtil.success("更新成功");
	    }
	    @ApiOperation(value = "保存用户并指定角色")
	    @PostMapping("saveWithRoleId")
	    public Message saveWithRoleId(BaseUserExtend baseUserExtend){
	    	iBaseUserService.addUserWithRoleId(baseUserExtend);
	        return MessageUtil.success("更新成功");
	    }
	    @ApiOperation(value = "根据用户ID更改用户头像")
	    @PostMapping("alterUserface")
	    public Message alterUserface(long id,String userface){
	    	iBaseUserService.updateUserfaceByUserId(id, userface);
	        return MessageUtil.success("更新成功");
	    }
	    @ApiOperation(value = "分页查询")
	    @GetMapping("pageQuery")
	    @ApiImplicitParams({
	        @ApiImplicitParam(name="page",value = "当前页",paramType = "query", required=true),
	        @ApiImplicitParam(name="pageSize",value = "页面大小",paramType = "query", required=true),
	    })
	    public Message pagequery(Long page, Long pageSize, String username, Long roleId, String status){
	    	List<BaseUserExtend> list = iBaseUserService.pagequery(page, pageSize, username, roleId, status);
	    	return MessageUtil.success(list);
	    }
	    @ApiOperation(value = "根据用户ID更改用户角色ID")
	    @PostMapping("setRoles")
	    @ApiImplicitParams({
	        @ApiImplicitParam(name="userId",value = "用户ID",paramType = "query", required=true),
	        @ApiImplicitParam(name="roleId",value = "角色ID",paramType = "query", required=true),
	    })
	    public Message setRoles(long userId,long roleId){
	    	iBaseUserRoleService.updateRoleIdByUserId(userId, roleId);
	        return MessageUtil.success("更新成功");
	    }
	    @ApiOperation(value = "通过用户id查找角色")
	    @GetMapping("cascadeRoleFindAll")
	    @ApiImplicitParams({
	        @ApiImplicitParam(name="id",value = "主键",paramType = "query", required=true),
	    })
	    public Message cascadeRoleFindAll(@NotNull Long id){
	    	List<BaseUserExtend> list =iBaseUserService.selectRoleByUserId(id);
	    	return MessageUtil.success(list);
	    }
}
